package com.weishenai.customerassistant.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Auther: weishenai
 * @Date: 2021/11/29/20:14
 * @Description:
 */
@Slf4j
@Component
public class MyBatisPlusObjectHandler implements MetaObjectHandler {
    //插入时的填充
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("star insert fill...");
        //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject)
        if (this.getFieldValByName("created", metaObject) == null) {
            this.setFieldValByName("created", LocalDateTime.now(), metaObject);
        }
        if (this.getFieldValByName("updated", metaObject) == null) {
            this.setFieldValByName("updated", LocalDateTime.now(), metaObject);
        }
    }

    //更新时填充
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("star update fill...");
        this.setFieldValByName("updated",LocalDateTime.now(), metaObject);
    }
}
